#include "../mqserver/mq_binding.hpp"
#include <gtest/gtest.h>

zlrmq::BindingManager::ptr bmp;

class BindingTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        bmp = std::make_shared<zlrmq::BindingManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        bmp->clear();
    }
};

TEST(binding_test, bind_test)
{
    bmp->bind("exchange1", "queue1", "news.music.#", true);
    bmp->bind("exchange1", "queue2", "news.sport.#", true);
    bmp->bind("exchange1", "queue3", "news.gossip.#", true);
    bmp->bind("exchange2", "queue1", "news.music.pop", true);
    bmp->bind("exchange2", "queue2", "news.sport.football", true);
    bmp->bind("exchange2", "queue3", "news.gossip.#", true);

    ASSERT_EQ(bmp->size(), 6);
}

TEST(binging_test, remove_test)
{
    bmp->removeExchangeBindings("exchange1");
    ASSERT_EQ(bmp->exists("exchange1", "queue1"), false);
    ASSERT_EQ(bmp->exists("exchange1", "queue2"), false);
    ASSERT_EQ(bmp->exists("exchange1", "queue3"), false);

    zlrmq::Binding::ptr bp = bmp->getBinding("exchange1", "queue1");
    ASSERT_EQ(bp.get(), nullptr);
}

TEST(binding_test, getBinding_test)
{
    auto it = bmp->getBinding("exchange2", "queue1");
    ASSERT_EQ(it->exchang_name, "exchange2");
    ASSERT_EQ(it->msgqueue_name, "queue1");
}

TEST(binding_test, getExchangeBindings_test)
{
    auto it = bmp->getExchangeBindings("exchange2");
    ASSERT_EQ(it.size(), 3);
}

TEST(binding_test, removeMsgQueueBinding_test)
{
    bmp->removeMsgQueueBinding("queue1");
    ASSERT_EQ(bmp->exists("exchange2", "queue1"), false);
}

TEST(binding_test, unbind_test)
{
    bmp->unBind("exchange2", "queue2");
    ASSERT_EQ(bmp->exists("exchange2", "queue2"), false);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new BindingTest);
    int ret = RUN_ALL_TESTS();
    (void)ret;
    return 0;
}